const fp = require('loadsh/fp')
const {Maybe, Container} = require('./support')

let maybe = Maybe.of([5, 6, 1])
// 练习1
let ex1 = () => {
    return maybe.map(list => {
        let remark = 0
        let num = fp.map(e => {
            return remark = fp.add(e, remark)
        }, list)
        return num[num.length - 1]
    })
}

// 练习2
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = () => {
    return xs.map(x => fp.first(x))
}

// 练习3
let safeProp = fp.curry(function(x, o) {
    return Maybe.of(o[x])
})
let user = {
    id: 2, name: "Albert"
}
let ex3 = () => {
    return safeProp('name', user).map(x => fp.first(x))
}

// 练习4
let ex4 = function (n) {
    return Maybe.if(n).map(x => x ? parseInt(x) : n)
}